package com.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.ArticleMapper;
import com.mapper.CSaveMapper;
import com.mapper.SysUserMapper;
import com.mapper.UserInfoMapper;
import com.pojo.dto.Message;
import com.pojo.entity.CArticle;
import com.pojo.entity.CSave;
import com.pojo.entity.SysUser;
import com.pojo.entity.SysUserInfo;
import com.pojo.vo.ArticleVo;
import com.pojo.vo.DiaoYouVo;
import com.pojo.vo.SelectVo;
import com.pojo.vo.UserVo;
import com.service.ArticleService;
import com.util.Approve;
import com.util.PojoUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    SysUserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    CSaveMapper saveMapper;
    @Autowired
    SysUtilServiceImpl sysUtilService;

    @Override
    public Message<List<ArticleVo>> getAllArticle(ArticleVo articleVo) {
        QueryWrapper wrapper=new QueryWrapper();
        if(articleVo.getTypeId()!=null){
          wrapper.eq("type_id",articleVo.getTypeId());
        }
        if(articleVo.getKeywords()!=null){
            wrapper.like("keywords",articleVo.getKeywords());
        }
        if(articleVo.getCity()!=null){
            wrapper.eq("city",articleVo.getCity());
        }
        if(articleVo.getUserNum()!=null){
            wrapper.eq("user_num",articleVo.getUserNum());
        }
        List<CArticle> list = articleMapper.selectList(wrapper);
        List<ArticleVo>articleVoList=new ArrayList<>();
        if(list.size()>0){
            list.forEach(v->{
                ArticleVo articleVo1 = PojoUtil.copyObject(v, ArticleVo.class);
                SysUser user = getUser(v.getUserNum());
                articleVo1.setUsername(user.getUsername());
                articleVo1.setTouxiang(getTouxaing(v.getUserNum()));
                articleVo1.setPicture(getPicture(v));
                articleVo1.setTypeName(getType(v));
                articleVo1.setSelected(true);
                articleVoList.add(articleVo1);
            });

            return Message.successMessage((long) articleVoList.size(),articleVoList);

        }
        return  Message.errorMessage("0000","查询失败或无数据");

    }

    @Override
    public Message<List<ArticleVo>> getArticlesById(Long id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_num",id);
        List<ArticleVo>listArticle = articleMapper.selectList(wrapper);
        List<CArticle> list = articleMapper.selectList(wrapper);
        List<ArticleVo>articleVoList=new ArrayList<>();
        if(list.size()>0){
            list.forEach(v->{
                ArticleVo articleVo1 = PojoUtil.copyObject(v, ArticleVo.class);
                SysUser user = getUser(v.getUserNum());
                articleVo1.setUsername(user.getUsername());
                articleVo1.setPicture(getPicture(v));
                articleVo1.setTypeName(getType(v));
                articleVo1.setSelected(true);
                articleVoList.add(articleVo1);
            });
            return Message.successMessage((long) articleVoList.size(),articleVoList);
        }
        return  Message.errorMessage("0000","查询失败或无数据");
    }

    @Override
    public Message<ArticleVo> getArticle(ArticleVo articleVo) {
    QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",articleVo.getId());
        CArticle cArticle = articleMapper.selectOne(wrapper);
        ArticleVo articleVo1 = PojoUtil.copyObject(cArticle, ArticleVo.class);
        articleVo1.setTouxiang(getTouxaing(cArticle.getUserNum()));
        articleVo1.setPicture(getPicture(cArticle));
        articleVo1.setTypeName(getType(cArticle));
        articleVo1.setSelected(true);
        return Message.successMessage(1L,articleVo1);
    }

    @Override
    public Message<String> deleteArticleById(Long id) {
        int cArticle = articleMapper.deleteById(id);
        if(cArticle>0) {
            return Message.successMessage(1L, "删除成功");
        }
        return Message.errorMessage("0000","删除失败");
    }

    @Override
    public Message<String> addArticle(ArticleVo article) {
        CArticle cArticle = PojoUtil.copyObject(article, CArticle.class);
        String s=cArticle.getTitle()+cArticle.getKeywords()+cArticle.getContent();
        JSONObject sampletext = Approve.sampletext(s);
        if (sampletext.get("content").equals("合规")) {
            int insert = articleMapper.insert(cArticle);
            if (insert > 0) {
                return Message.successMessage(1L, "添加成功");
            }
            return Message.errorMessage("0000", "添加失败");
        }
        return Message.errorMessage("0000", "文本不合规");
    }

    @Override
    public Message<ArticleVo> updateArticle(ArticleVo article) {
        CArticle cArticle = PojoUtil.copyObject(article, CArticle.class);
        int update = articleMapper.updateById(cArticle);
        if(update>0){
            return getArticle(article);
        }
        return Message.errorMessage("0000","修改失败");
    }

    @Override
    public Message<String> saveArticle(CSave cSave) {
        int insert = saveMapper.insert(cSave);
        if(insert>0){
            return Message.successMessage(1L, "收藏成功");
        }
        return Message.errorMessage("0000","收藏失败");
    }

    @Override
    public Message<String> attention(CSave cSave) {
        int insert = saveMapper.insert(cSave);
        if(insert>0){
            return Message.successMessage(1L, "关注成功");
        }
        return Message.errorMessage("0000","关注失败");
    }

    public SysUser getUser(Long id){
        SysUser sysUser = userMapper.selectById(id);
        return sysUser;
    }

    public String getTouxaing(Long id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_num",id);
        SysUserInfo sysUserInfo = userInfoMapper.selectOne(wrapper);
        return sysUserInfo.getPath();
    }
    public String[] getPicture(CArticle vo){
        String picturePath = vo.getPicturePath();
        String[] split = picturePath.split("&&&");
        return split;
    }
    public String getType(CArticle vo){
        SelectVo article_type = sysUtilService.selectone(vo.getTypeId(), "article_type");
        return article_type.getText();
        // return split;
    }

   /* public Message<List<DiaoYouVo>> getDiaoYou(){
        List<SysUser> sysUsers = userMapper.selectList(null);

        List<CSave> cSaves = saveMapper.selectList(null);

    }*/
}
